﻿using System;
using System.Reflection;

namespace InfiniTec
{
    public static class ExceptionHelper
    {
        private static readonly MethodInfo _PrepForRemotingMethod;

        static ExceptionHelper()
        {
            try
            {
                _PrepForRemotingMethod = typeof(Exception).GetMethod("PrepForRemoting",
                                                                                 BindingFlags.Instance | BindingFlags.NonPublic);
            }
            catch (Exception)
            {
                _PrepForRemotingMethod = null;
            }
        }

        public static bool PreserveExceptionStackTrace(Exception exception)
        {
            if (_PrepForRemotingMethod != null)
            {
                _PrepForRemotingMethod.Invoke(exception, null);
                return true;
            }
            return false;
        }
    }
}